﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BO.Management.PowerShell.InternalHelper
{
    internal static class DiscoverHelper
    {
        internal static String GetObjectProperties(Object objectToDiscover)
        {
            StringBuilder discoverProps = new StringBuilder();
            discoverProps.AppendLine("===================== Start Properties Auto discovery ===============================");
            
            if (objectToDiscover == null)
            {
                discoverProps.AppendLine("Object is null");
            }
            else
            {
                System.ComponentModel.PropertyDescriptorCollection props = System.ComponentModel.TypeDescriptor.GetProperties(objectToDiscover);

                foreach (System.ComponentModel.PropertyDescriptor prop in props)
                {
                    Object value = null;

                    try
                    {
                        value = prop.GetValue(objectToDiscover);
                    }
                    catch (Exception ex)
                    {
                        value = ex.Message;
                    }

                    try
                    {
                        discoverProps.AppendLine(String.Format("Property {0} : {1}", prop.Name, value.ToString()));
                    }
                    catch (Exception ex)
                    {
                        discoverProps.AppendLine("Error : " + ex.Message);
                    }
                }
            }
            discoverProps.AppendLine("===================== End Properties Auto discovery ===============================");
            
            return discoverProps.ToString();
            
        }

    }
}
